package smartcampus.scjava.controller.forth;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import smartcampus.scjava.POJO.DO.Book;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.IBookService;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 书籍表 前端控制器
 * </p>
 *
 * @author xian
 * @since 2023-11-22
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Resource
    private IBookService bookService;


    @RequestMapping("/listPage")
    public ResultVO<?> listPageBook(Integer  pageNum,Integer pageSize,String word) {
        if (pageNum == null || pageSize == null) {
            return ResultVO.error("分页参数错误");
        }

        Page<Book> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Book> query = new QueryWrapper<>();
        if (StringUtils.hasText(word)) {
            query.lambda().like(Book::getBookName,word).
                    or().like(Book::getBookAuthor,word).
                    or().like(Book::getBookNo,word);
        }


        bookService.page(page,query);

        return ResultVO.success(page);
    }

    @RequestMapping(value = "/getOne")
    public ResultVO<Book> getOne(@RequestBody Book book) {

        QueryWrapper<Book> query = new QueryWrapper<>(book);
        return ResultVO.success(bookService.getOne(query));
    }

}
